package com.example.cameraxpush;


import android.app.Activity;
import android.graphics.ImageFormat;
import android.hardware.Camera;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;

import androidx.annotation.NonNull;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

// camera1  帮助类
public class CameraOneHelper implements SurfaceHolder.Callback, Camera.PreviewCallback {

    private static final  String TAG = "CameraOneHelper";
    Camera mCamera;
    int mCameraId;
    // 自己设置的分辨率  后面会寻找 最匹配的支持分辨率
    int width;
    int height;

    Activity activity;

    int rotation;

    byte[] buffer;
    private SurfaceHolder surfaceHolder;


    public CameraOneHelper( int width, int height, Activity activity) {
        this.mCameraId = Camera.CameraInfo.CAMERA_FACING_BACK;
        this.width = width;
        this.height = height;
        this.activity = activity;
    }

    private void startPreview() {


        mCamera = Camera.open(mCameraId);

        // 配置camera 属性
        Camera.Parameters parameters = mCamera.getParameters();
        parameters.setPreviewFormat(ImageFormat.NV21);

        //  找到支持的分辨率 设置预览尺寸
        setPreviewSize(parameters);

        // 设置摄像头 图像传感器的角度 方向
        setPreviewOrientation(parameters);

        // 设置属性
        mCamera.setParameters(parameters);

        buffer = new byte[width * height * 3 / 2];
        byte[] yuv = new byte[width * height * 3 / 2];

        mCamera.addCallbackBuffer(buffer);

        mCamera.setPreviewCallbackWithBuffer(this);

        try {
            mCamera.setPreviewDisplay(surfaceHolder);
            mCamera.startPreview();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }



    private void stopPreview() {

        if (mCamera != null) {
            mCamera.setPreviewCallbackWithBuffer(null);
            mCamera.stopPreview();
            mCamera.release();
            mCamera = null;
        }
    }

    public void switchCamera() {
        if (mCameraId == Camera.CameraInfo.CAMERA_FACING_BACK) {
            mCameraId = Camera.CameraInfo.CAMERA_FACING_FRONT;
        }else  {
            mCameraId = Camera.CameraInfo.CAMERA_FACING_BACK;
        }
        // 停止
        stopPreview();
        // 开始
        startPreview();
    }
    private void setPreviewOrientation(Camera.Parameters parameters) {

        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        Camera.getCameraInfo(mCameraId,cameraInfo);

        rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
        int degress = 0;
        switch (rotation) {
            case Surface.ROTATION_0:
                degress = 0;
                break;
            case Surface.ROTATION_90:
                degress = 90;
                break;
            case Surface.ROTATION_180:
                degress = 180;
                break;
            case Surface.ROTATION_270:
                degress = 270;
                break;
        }
        int result;
        if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
            result = (cameraInfo.orientation + degress) % 360;
            result = (360-result) % 360;
        }else {
            result = (cameraInfo.orientation - degress + 360) % 360;
        }
        mCamera.setDisplayOrientation(result);
    }

    private void setPreviewSize(Camera.Parameters parameters) {

        List<Camera.Size> supportedPreviewSizes = parameters.getSupportedPreviewSizes();
        Camera.Size size = supportedPreviewSizes.get(0);
        Log.e(TAG,"支持："+ size.width + "X" + size.height);

        // 选择一个与设置的 差距最小的分辨率
        int m= Math.abs(size.height * size.width - width * height);

        supportedPreviewSizes.remove(0);
        Iterator<Camera.Size> iterator = supportedPreviewSizes.iterator();

        while (iterator.hasNext()) {

            Camera.Size next = iterator.next();
            Log.e(TAG,"支持："+ size.width + "X" + size.height);
            int n= Math.abs(size.height * size.width - width * height);
            // 比 M 小 保存
            if (n < m) {
                m = n ;
                size = next;
            }
        }

        width = size.width;
        height = size.height;

        parameters.setPreviewSize(width,height);

        Log.e(TAG," 设置预览尺寸："+ size.width + "X" + size.height);
    }

    public void setPreviewDisplay(SurfaceHolder surfaceHolder) {
        this.surfaceHolder = surfaceHolder;
        surfaceHolder.addCallback(this);
    }
    @Override
    public void surfaceCreated(@NonNull SurfaceHolder holder) {

    }

    @Override
    public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {

        // 释放
        stopPreview();
        startPreview();
    }

    @Override
    public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
// 释放
        stopPreview();
    }

    @Override
    public void onPreviewFrame(byte[] data, Camera camera) {
        // camera 回调回来的数据  data依然需要旋转



        camera.addCallbackBuffer(buffer);

    }
}
